﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;

using System.Threading;
using Microsoft.Win32;

namespace AMPLibrary.Registry
{
    public class FileAssociations
    {
        // a string that will hold the name of the settings file
        public static string SettingsSaveFile
        {
            get;
            private set;
        }

        // an object that gets the current operating system used by the application
        public static OperatingSystem OperatingSystem
        {
            get;
            private set;
        }

        // an object that contains information about who is using the application
        public static WindowsIdentity WindowsIdentity
        {
            get;
            private set;
        }

        /// <summary>
        /// 
        /// </summary>
        private static void SetPaths()
        {
            string applicationPath = System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("AMP.exe", string.Empty);

            switch (OperatingSystem.Version.Major)
            {
                case 5: // windows xp
                    SettingsSaveFile = applicationPath + ApplicationFile.Settings;
                    break;

                case 6://vista and 7
                case 7:
                    string username = WindowsIdentity.Name.Split('\\')[1];
                    string systemDrive = Environment.GetEnvironmentVariable("windir").ToCharArray()[0].ToString();
                    string applicationData = systemDrive + ":\\users\\" + username + "\\appData\\local\\Adamo Technology\\AMP\\";

                    if (Directory.Exists(systemDrive + ":\\users\\" + username + "\\appData\\"))
                    {
                        if (!Directory.Exists(applicationData))
                            Directory.CreateDirectory(applicationData);

                        SettingsSaveFile = applicationData + ApplicationFile.Settings;
                    }
                    else
                    {
                    }
                    break;

                default:
                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static void BindExtensionsToRegistry()
        {
            try
            {
                FileStream inputFileStream = File.Open(SettingsSaveFile, FileMode.Open);
                StreamReader streamReader = new StreamReader(inputFileStream);

                string line = streamReader.ReadLine();
                while (line != null)
                {
                    string[] keyValuePair = line.Split('=');

                    switch (keyValuePair[0])
                    {
                        case "AssociateMP3":
                            if (Convert.ToBoolean(keyValuePair[1]))
                            {
                                try
                                {
                                    //RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + extension + "\\UserChoice", true);
                                    RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.mp3\\UserChoice");
                                    key.SetValue("Progid", "Applications\\AMP.exe");
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                    System.Threading.Thread.Sleep(3000);
                                }
                            }
                            break;

                        case "AssociateAVI":
                            if (Convert.ToBoolean(keyValuePair[1]))
                            {
                                try
                                {
                                    //RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + extension + "\\UserChoice", true);
                                    RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.avi\\UserChoice");
                                    key.SetValue("Progid", "Applications\\AMP.exe");
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                    System.Threading.Thread.Sleep(3000);
                                }
                            }
                            break;

                        case "AssociateM3U":
                            if (Convert.ToBoolean(keyValuePair[1]))
                            {
                                try
                                {
                                    //RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + extension + "\\UserChoice", true);
                                    RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.m3u\\UserChoice");
                                    key.SetValue("Progid", "Applications\\AMP.exe");
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                    System.Threading.Thread.Sleep(3000);
                                }
                            }
                            break;
                    }

                    line = streamReader.ReadLine();
                }

                streamReader.Close();
                inputFileStream.Close();
            }
            catch
            {

            }
        }
    }
}
